<?php

	class VegetablesService{
		
		private $db;
		
		function VegetablesService($db = NULL){
			if($db){
				$this->db = $db;
			}
			
			if(!$this->db){
				require_once 'plugins/db.php';
				$this->db = new Db();
			}
			
		}
		
		function getVegetablesListByCateId($cateId = -1, $page = 1, $pageSize = 20){
			
			$list = $this->getVegetables($cateId, $page, $pageSize);
			$recordCount = $this->getVegetablesCountByCateId($cateId);
			
			$pageCount = $recordCount/$pageSize;
			$pageCount += $recordCount%$pageSize > 0 ? 1 : 0;
			
			return array('list'=>$list, 'page_count'=>$pageCount, 'page'=>$page);
		}
		
		private function getVegetables($cateId, $page, $pageSize){
			$sql = "select * from dragon_fancai_vegetables ";
			if($cateId >= 0){
				$sql .= " where cate_id=".addslashes($cateId);
			}
			return $this->db->fetchPage($sql, $page, $pageSize);
		}
		
		function getVegetablesCountByCateId($cateId){
			
			$sql = "select count(1) from dragon_fancai_vegetables ";
			if($cateId >= 0){
				$sql .= " where cate_id=".addslashes($cateId);
			}
			return $this->db->fetchOne($sql);
		}
		
		
	}

?>